<template>
	<view>
		<u-tabs v-if="language=='cn'" :list="list1" lineColor="#10A998" lineWidth="20" :activeStyle="{
		            color: '#303133',
		            fontWeight: 'bold',
		            transform: 'scale(1.05)'
		        }" :inactiveStyle="{
		            color: '#606266',
		            transform: 'scale(1)'
		        }" itemStyle="padding-left: 30rpx; padding-right: 30rpx; height: 80rpx;" @click="click">
		</u-tabs>
		<u-tabs v-else :list="list2" lineColor="#10A998" lineWidth="20" :activeStyle="{
		            color: '#303133',
		            fontWeight: 'bold',
		            transform: 'scale(1.05)'
		        }" :inactiveStyle="{
		            color: '#606266',
		            transform: 'scale(1)'
		        }" itemStyle="padding-left: 30rpx; padding-right: 30rpx; height: 80rpx;" @click="click">
		</u-tabs>
		<block v-if="current==0">
			<view class="con">
				<!-- <view style="width: 750rpx; height: 420rpx;" @click="uploadFilePromises">
				<image :src="URL(params.image)" class="addpicbox" v-if="params.image" mode=""></image>
				<view class="addpicbox" v-else>
					<image src="/static/zhibo/addpic.png" class="addpicicon" mode="aspectFill"></image>
					添加封面图
				</view>
			</view> -->

				<view class="form">
					<view class="form__item">
						<view class="form__item__label">
							{{$getText('直播封面') }}

						</view>
						<image @click="uploadFilePromises()" v-if="params.image"
							style="width: 240rpx;height: 240rpx;border-radius: 15rpx;margin: 20rpx 0;"
							:src="URL(params.image)"></image>
						<view class="add" v-else @click="uploadFilePromises()">
							<image src="../../static/home/plus.png" mode="widthFix"></image>
							<text>{{$getText('上传封面') }}</text>
						</view>
					</view>
					<view class="form__item">
						<!-- #ifdef H5 -->
						<view class="form__item__label"
							@click="params.image='https://c-ssl.duitang.com/uploads/blog/202304/26/20230426203241_1da8e.jpeg'">
							直播名称
						</view>
						<!-- #endif -->
						<!-- @click="params.image='https://c-ssl.duitang.com/uploads/blog/202304/26/20230426203241_1da8e.jpeg'" -->
						<!-- #ifdef APP-PLUS -->
						<view class="form__item__label">

							{{$getText('直播名称') }}
						</view>
						<!-- #endif -->
						<view class="form__item__inp">
							<input type="text" v-model="params.title" :placeholder="$getText('请填写')"
								placeholder-style="color:#999;">
						</view>
					</view>
					<view class="form__item">
						<view class="form__item__label">
							直播方式
						</view>
						<view class="form__item__inp" style="display: flex;align-items: center;">
							<view @click="params.subtype=1" style="display: flex;align-items: center;">
								<radio :checked="params.subtype==1"> </radio>{{$getText('立即直播') }}
							</view>
							<view @click="params.subtype=2" style="display: flex;align-items: center;">
								<radio :checked="params.subtype==2" style="margin-left: 60px;"> </radio>
								{{$getText('预约直播') }}
							</view>
						</view>
					</view>
					<view class="form__item" v-if="params.subtype!=1">
						<view class="form__item__label">
							{{$getText('直播时间') }}

						</view>
						<view class="form__item__inp" @click="show=true">
							{{params.appointtime||'请填写'}}
						</view>
					</view>
					<view class="form__item">
						<view class="form__item__label">
							{{$getText('直播内容') }}

						</view>
						<view class="form__item__inp">
							<u--textarea v-model="params.content" :placeholder="$getText('请填写')"
								border='none'></u--textarea>
						</view>
					</view>
				</view>
			</view>
			<view class="footer" @click="submit()">
				<u-button :text="$getText('发布') " color="#4BD3C7"></u-button>
			</view>
		</block>
		<block v-if="current==1">
			<view class="tab2">
				<view class="content">
					<view class="u-flex-items-center u-flex">
						<view class="label">

							{{$getText('标题') }}
						</view>
						<view class="u-flex-items-center u-flex u-flex-content-end " style="flex: 1;">
							<u--input v-model="model.title" inputAlign="right" border="none"
								:placeholder="$getText('请输入标题')"></u--input> <u-icon color="#888"
								name="arrow-right"></u-icon>
						</view>
					</view>
					<view class="textarea" style="">
						<u--textarea border="none" v-model="model.intro"
							:placeholder="$getText('请输入内容') "></u--textarea>

					</view>
					<view class="sm">
						<text></text>{{$getText('上传封面') }}
					</view>
					<image @click="uploadFilePromise()" v-if="model.image"
						style="width: 240rpx;height: 240rpx;border-radius: 15rpx;margin: 20rpx 0;"
						:src="URL(model.image)"></image>
					<view class="add" v-else @click="uploadFilePromise()">
						<image src="../../static/home/plus.png" mode="widthFix"></image>
						<text>{{$getText('上传封面') }}</text>
					</view>
					<view class="sm">
						<text></text>{{$getText('发布视频') }}
					</view>
					<video style="margin: 20rpx 0;border-radius: 15rpx;" v-if="model.uri" :src="URL(model.uri)"></video>
					<view class="add" v-else @click="uploadFilePromise('file')">
						<image src="../../static/home/plus.png" mode="widthFix"></image>
						<text>{{$getText('发布视频') }} </text>
					</view>
					<view class="label">
						{{$getText('商品下单链接') }}
					</view>
					<view class="goodsList">
						<view class="goodsItem" v-for="(i,n) in goodsList" :key="n">
							<image :src="URL(i.image)" mode=""></image>
							<view class="" style="flex: 1;">
								<view class="">
									{{i.name}}
								</view>
								<text class="price">￥{{i.price}}</text>
							</view>
							<view class="del" @click="del(n)">
								{{$getText('删除') }}

							</view>
						</view>
					</view>
					<navigator v-if="goodsList.length==0" url="/pages/chooseGoods/list" hover-class="none"
						class="addgoods">
						<image src="../../static/home/plus1.png"></image>{{$getText('添加商品') }}
					</navigator>
				</view>
				<view class="" style="height: 162rpx;">

				</view>
				<view class="submit" @click="submits">
					<view class="btn">
						{{$getText('确定发布') }}
					</view>
				</view>
			</view>


		</block>
		<u-datetime-picker :filter="dataFileter" @cancel="show=false" @confirm="show=false" :show="show"
			v-model="params.appointtime" mode="time"></u-datetime-picker>
	</view>
</template>

<script>
	import {
		createRoom,
		createVideo
	} from "@/request2/api/goods.js"
	export default {
		data() {
			return {
				show: false,
				fileList6: [],
				params: {
					subtype: 1,
					title: "",
					image: '',
					content: '',
					appointtime: ''
				},
				list1: [{
						name: "创建直播",
					},
					{
						name: "短视频",
					}
				],
				list2: [{
						name: "اشىۋ كورسەتۋ تىكەلەي "
					},
					{
						name: "كەسكىن قىسقا"
					}
				],

				model: {
					title: '',
					intro: "",
					image: "",
					uri: "",
					goods_id: ""
				},
				goodsList: [],
				current: 0,
				language: null
			};
		},
		mounted() {
			// if (!a) {
			// 	uni.setStorageSync('language', 'cn')
			// }
			this.language = uni.getStorageSync('language');
			let vm = this
			uni.$on('emitGoods', function(data) {
				vm.goodsList.push(data)
				vm.model.goods_id = data.id
			})
		},
		onUnload() {
			uni.$off('emitGoods')
			let a = uni.getStorageSync('language');

		},
		computed: {
			URL() {
				return (url) => {
					// if (!url) return require('@/static/logo.png')
					if (url && (url.includes(',') && !url.includes('base64'))) url = url.split(',')[0]
					return url ? ((url.includes('http') || url.includes('data:')) ? url : 'http://wg.fuliaoxx.com' +
						url) : ''
				}
			},
		},
		methods: {
			dataFileter(type,arr) {
				let h= new Date().getHours()
				if(type=="hour"){
					return arr.filter(v=>v>h)
				}else{
					return arr
				}
			},
			del(n) {
				this.goodsList.splice(n, 1)
			},
			click(e) {
				console.log(e)
				this.current = e.index
			},
			async submits() {
				if (!this.model.title) {
					return this.msg("请输入标题!")
				}
				if (!this.model.image) {
					return this.msg("请上传封面!")
				}
				if (!this.model.uri) {
					return this.msg("请上传视频!")
				}
				const {
					data
				} = await createVideo(this.model)
				console.log(data)
				if (data.id) {
					setTimeout(() => {
						uni.navigateBack()
					}, 1000)
				}

			},
			async submit() {
				this.params.room_id = this.$store.state.user.userInfo.id
				// this.params.appointtime = new Date().getTime()
				if (!this.params.image) {
					return this.msg("请上传图片!")
				}
				if (!this.params.appointtime && this.params.subtype == 2) {
					return this.msg("请选择直播时间!")
				}
				if (!this.params.content) {
					return this.msg("请输入直播内容!")
				}
				if (!this.params.title) {
					return this.msg("请输入直播标题!")
				}
				console.log("创建房间参数-------------------------------")
				console.log(this.params)
				// return false
				const {
					data
				} = await createRoom(this.params)
				console.log(data)
				// return false;
				if (data && data.room_id) {

					console.log(this.$store.state.user.userInfo.id)
					if (this.params.subtype == 2) {
						uni.showToast({
							icon: 'none',
							title: '创建成功,请记得准时直播!'
						})
						return false
					}
					uni.showToast({
						icon: 'none',
						title: '创建直播成功!'
					})
					uni.redirectTo({
						url: '/pages/live/liveshow?role=20&roomId=' + this.$store.state.user.userInfo.id
					})
				}
			},

			uploadFilePromises(url) {
				let that = this
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.showLoading({
							title: '正在上传'
						})
						uni.uploadFile({
							url: 'http://wg.fuliaoxx.com/api/common/upload', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							header: {
								"token": uni.getStorageSync('token') ? uni.getStorageSync('token') :
									"",
							},
							success: (uploadFileRes) => {
								uni.hideLoading()
								// console.log(JSON.parse(uploadFileRes.data));
								if (JSON.parse(uploadFileRes.data).code == -1) {
									uni.showToast({
										title: JSON.parse(uploadFileRes
												.data)
											.msg,
										icon: 'none'
									})
									uni.redirectTo({
										url: '/pages/login/codelogin'
									})
									return
								}
								if (JSON.parse(uploadFileRes.data).code == 1) {

									uni.showToast({
										title: JSON.parse(uploadFileRes.data).msg,
										icon: 'none'
									})
									that.params.image = JSON.parse(uploadFileRes.data).data.url
								} else {
									uni.showToast({
										title: JSON.parse(uploadFileRes.data).msg,
										icon: 'none'
									})
								}
							}
						});
					}
				});
			},
			uploadFilePromise(type) {
				let that = this
				uni[type == 'file' ? 'chooseVideo' : 'chooseImage']({
					success: (chooseImageRes) => {
						const tempFilePaths = type == 'file' ? chooseImageRes.tempFilePath : chooseImageRes
							.tempFilePaths[0];
						uni.showLoading({
							title: '正在上传'
						})
						console.log(chooseImageRes)
						uni.uploadFile({
							url: 'http://wg.fuliaoxx.com/api/common/upload', //仅为示例，非真实的接口地址
							filePath: tempFilePaths,
							name: 'file',
							header: {
								"token": uni.getStorageSync('token') ? uni.getStorageSync('token') :
									"",
							},
							success: (uploadFileRes) => {
								uni.hideLoading()
								// console.log(JSON.parse(uploadFileRes.data));
								if (JSON.parse(uploadFileRes.data).code == -1) {
									uni.showToast({
										title: JSON.parse(uploadFileRes
												.data)
											.msg,
										icon: 'none'
									})
									uni.redirectTo({
										url: '/pages/login/codelogin'
									})
									return
								}
								console.log(JSON.parse(uploadFileRes.data))
								if (JSON.parse(uploadFileRes.data).code == 1) {

									uni.showToast({
										title: JSON.parse(uploadFileRes.data).msg,
										icon: 'none'
									})
									if (type == 'file') {
										that.model.uri = JSON.parse(uploadFileRes.data).data.url
									} else {
										that.model.image = JSON.parse(uploadFileRes.data).data.url
									}


									this.$forceUpdate()
								} else {
									uni.showToast({
										title: JSON.parse(uploadFileRes.data).msg,
										icon: 'none'
									})
								}
							}
						});
					}
				});
			},
		}
	}
</script>

<style lang="less">
	page {
		background-color: #fff;
	}

	.con {
		padding-top: 20rpx;
		background-color: #F5F5F7;
	}

	.addpicbox {

		width: 750rpx;
		height: 420rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #EEEEEE;
		flex-direction: column;
		font-size: 28rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #999999;
		line-height: 39rpx;

		.addpicicon {
			width: 140rpx;
			height: 140rpx;
			margin-bottom: 13rpx;
		}
	}

	.form {
		background-color: #fff;
		padding: 0 28rpx;

		&__item {
			padding: 28rpx 0 20rpx;
			border-bottom: 1rpx solid #EEEEEE;

			&__label {
				font-size: 32rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #333333;
				line-height: 45rpx;
				margin-bottom: 20rpx;
			}

			&__inp {
				font-size: 32rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #333;
				line-height: 45rpx;
			}
		}
	}

	::v-deep.uni-textarea-compute {

		div {
			font-size: 32rpx;
		}
	}

	::v-deep.u-textarea {
		padding: 0;
	}

	.uni-input-placeholder {
		color: #999999;
	}

	.input-placeholder {
		color: #999999 !important;
		font-size: 32rpx;
	}

	.footer {
		width: 694rpx;
		padding: 12rpx 28rpx;
		height: 86rpx;
		background: #FFFFFF;
		position: fixed;
		bottom: 0;
		left: 0;
	}

	.tab2 {
		background: #f8f8f8;
		padding: 28rpx 0;

		.content {
			margin: 0 30rpx;
			background-color: #fff;
			border-radius: 30rpx;
			padding: 60rpx 24rpx 34rpx 30rpx;
		}
	}

	.submit {
		background-color: #fff;
		position: fixed;
		left: 0;
		width: 100%;
		bottom: 0;
		height: 162rpx;
		border-radius: 30rpx 30rpx 0 0;
		display: flex;
		align-items: center;
		justify-content: center;

		.btn {
			background-color: #10A998;
			width: 400rpx;
			height: 88rpx;
			line-height: 88rpx;
			color: #fff;
			border-radius: 50rpx;
			text-align: center;
			font-size: 32rpx;
		}
	}

	.label {
		font-weight: bold;
		font-size: 30rpx;
		color: #000000;
	}

	.textarea {
		border-bottom: 1rpx solid #f8f8f8;
		border-top: 1rpx solid #f8f8f8;
		margin-top: 45rpx;
		padding-top: 45rpx;
		height: 380rpx;
		box-sizing: border-box;
	}

	.sm {
		display: flex;
		align-items: center;

		text {
			display: inline-block;
			width: 9rpx;
			height: 32rpx;
			background: #10A998;
			border-radius: 4rpx 4rpx 4rpx 4rpx;
			margin-right: 10rpx;
		}

		font-weight: bold;
		font-size: 30rpx;
		color: #010101;
		line-height: 30rpx;
		margin-top: 36rpx;
	}

	.add {
		width: 220rpx;
		height: 220rpx;
		border-radius: 15rpx;
		background: #f8f8f8;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin: 24rpx 0 48rpx;
		color: #888;

		image {
			width: 48rpx;

		}
	}

	.goodsList {
		margin-top: 46rpx;
	}

	.goodsItem {
		display: flex;
		background: #F8F8F8;
		border-radius: 20rpx;
		padding: 0 20rpx;
		align-items: center;
		justify-content: space-between;
		height: 122rpx;
		font-weight: 500;
		font-size: 32rpx;
		margin-bottom: 32rpx;
		color: #181818;

		image {
			width: 90rpx;
			height: 90rpx;
			border-radius: 15rpx;
			margin-right: 16rpx;
		}

		.del {
			width: 142rpx;
			height: 62rpx;
			background: #CCCCCC;
			border-radius: 12rpx;
			border: 2rpx solid #CCCCCC;
			font-weight: 500;
			font-size: 26rpx;
			color: #434343;
			display: flex;
			align-items: center;
			justify-content: center;
			text-align: center;
			box-sizing: border-box;
		}

		.price {
			color: #F54D42;
			font-size: 30rpx;
		}
	}

	.addgoods {
		image {
			width: 32rpx;
			height: 32rpx;
		}

		background: #ECFDEC;
		height: 93rpx;
		border-radius: 20rpx;
		font-weight: bold;
		font-size: 30rpx;
		color: #2F3931;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>